#include "MediaFile.h"
#include "MediaFileChild.h"
#include <string.h>
int GetFileTypeByFileName(const char*filename){
    const char*Pos=strrchr(filename,'.');
    if(Pos==NULL)return FILE_TYPE_NONE;
    if(strcmpi(Pos+1,"yuv")==0)return FILE_TYPE_YUV;
    if(strcmpi(Pos+1,"264")==0)return FILE_TYPE_AVC;
    if(strcmpi(Pos+1,"h264")==0)return FILE_TYPE_AVC;
    if(strcmpi(Pos+1,"x264")==0)return FILE_TYPE_AVC;
    if(strcmpi(Pos+1,"wav")==0)return FILE_TYPE_WAV;
    if(strcmpi(Pos+1,"aac")==0)return FILE_TYPE_AAC;
    if(strcmpi(Pos+1,"flv")==0)return FILE_TYPE_FLV;
    return FILE_TYPE_NONE;
}
MediaFile*MallocMediaFile(){
    MediaFile*mf=(MediaFile*)malloc(sizeof(MediaFile));
    memset(mf,0,sizeof(MediaFile));
    memset(&mf->AVCInfo,0,sizeof(mf->AVCInfo));
    memset(&mf->AACInfo,0,sizeof(mf->AACInfo));
    return mf;
}
MediaFile*OpenMediaFile(const char*filename){
    int FileType=GetFileTypeByFileName(filename);
    if(FileType==FILE_TYPE_NONE)return NULL;
    if(FileType==FILE_TYPE_AAC)return OpenMediaFileWithAAC(filename,FileType);
    if(FileType==FILE_TYPE_AVC)return OpenMediaFileWithAVC(filename,FileType);
    return NULL;
}
void CloseMediaFile(MediaFile*mf){
    if(mf->AVCInfo.spslen>0)free(mf->AVCInfo.sps);
    if(mf->AVCInfo.ppslen>0)free(mf->AVCInfo.pps);
    if(mf->AACInfo.aaclen>0)free(mf->AACInfo.aac);
    while(mf->FrameList){
      mf->CurrentFrame=mf->FrameList->NextFrame;
      free(mf->FrameList);
      mf->FrameList=mf->CurrentFrame;
    }
    fclose(mf->fp);
    free(mf);
}

MediaFrame*MallocMediaFrame(uint32_t malloclen){
    MediaFrame*frame=(MediaFrame*)malloc(sizeof(MediaFrame));
    memset(frame,0,sizeof(MediaFrame));
    frame->malloclen=malloclen;
    if(frame->malloclen>0)
       frame->data=(char*)malloc(frame->malloclen);
    return frame;
}
MediaFrame*ReallocMediaFrame(MediaFrame*frame,uint32_t malloclen){
    if(frame->malloclen<malloclen){
       frame->malloclen=malloclen;
       frame->data=(char*)realloc(frame->data,frame->malloclen);
    }
    return frame;
}

void FreeMediaFrame(MediaFrame*frame){
    if(frame==NULL)return;
    if(frame->malloclen>0)
       free(frame->data);
    free(frame);
}

void AddMediaFrame(MediaFile*mf,MediaFrame*frame){
    if(mf->CurrentFrame==NULL){
       mf->FrameList=frame;
       mf->CurrentFrame=frame;
    }else{
       mf->CurrentFrame->NextFrame=frame;
       mf->CurrentFrame=mf->CurrentFrame->NextFrame;
    }
}

MediaFrame*GetNextMediaFrame(MediaFile*mf){
    MediaFrame*frame=NULL;
    if(mf->CurrentFrame==NULL)return NULL;
    frame=MallocMediaFrame(mf->CurrentFrame->datalen);
    frame->CodecType=mf->CurrentFrame->CodecType;
    frame->FrameType=mf->CurrentFrame->FrameType;
    frame->KeyFrame=mf->CurrentFrame->KeyFrame;
    frame->timestamp=mf->CurrentFrame->timestamp;
    frame->datalen=mf->CurrentFrame->datalen;
    fseek(mf->fp,mf->CurrentFrame->pos,SEEK_SET);
    fread(frame->data,1,frame->datalen,mf->fp);
    mf->CurrentFrame=mf->CurrentFrame->NextFrame;
    return frame;
}